package ru.urfc.portal.ui.client.googlets.client;

import ru.urfc.portal.ui.client.BaseGooglet;
import ru.urfc.portal.ui.client.Style;
import ru.urfc.portal.ui.client.descriptor.GridDescriptor;
import ru.urfc.portal.ui.client.model.catalogs.NetLicenseState;
import ru.urfc.portal.ui.client.model.catalogs.NetState;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Widget;

public class NetInfo extends BaseGooglet {

	interface Binder extends UiBinder<Widget, NetInfo> {}
	private static Binder binder = GWT.create(Binder.class);
	
	@UiField Style style;
	@UiField Element name;
	@UiField Element region;
	@UiField Element state;
	@UiField Element channelTemplate;
	@UiField Element channels;
	@UiField Element tmpItemIn;
	@UiField Element tmpItemOut;
	@UiField Element tmpItemChannel;
	
	@UiField Element licenses;
	@UiField Element licenseTemplate;
	@UiField Element tmpLicenseNumber;
	@UiField Element tmpLicenseIssueDate;
	@UiField Element tmpLicenseExpireDate;
	@UiField Element licenseState;

	@Override
	protected void init() {
		initWidget(binder.createAndBindUi(this));
		
		GridDescriptor descriptor = new GridDescriptor(NET, CLIENTS, getClientId(), NETS, getItemId());
		descriptor.addTextField("name", name);
		descriptor.addTextField("region", region);
		descriptor.addCIFields("state/text", "state/code", NetState.class, state, style);
		createController(descriptor);
		
		initChannels();
		initLicenses();
	}

	private void initLicenses() {
		GridDescriptor descriptor = new GridDescriptor(LICENSE,
				CLIENTS, getClientId(), NETS, getItemId(), NET_LICENSES);
		descriptor.addTextField("number", tmpLicenseNumber); 
		descriptor.addDateField("issueDate", tmpLicenseIssueDate);
		descriptor.addDateField("expireDate", tmpLicenseExpireDate);
		descriptor.addCIFields("state/text", "state/code", NetLicenseState.class, licenseState, style);
		createController(descriptor, licenses, licenseTemplate);
	}

	private void initChannels() {
		GridDescriptor descriptor = new GridDescriptor(CHANNEL,
				CLIENTS, getClientId(), NETS, getItemId(), NET_CHANNELS);		
		descriptor.addIntField("in", tmpItemIn).getIntRenderer().setMHzFormat();
		descriptor.addIntField("out", tmpItemOut).getIntRenderer().setMHzFormat();
		descriptor.addIntField("channelNumber", tmpItemChannel);
		createController(descriptor, channels, channelTemplate);
	}

}